package portal.controller;

import beans.HttpConfig;
import beans.TokenValue;
import net.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import portal.util.PaserPage;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "SearchPageQueueListServlet")
public class SearchPageQueueListServlet extends HttpServlet {

    private static final long serialVersionUID = 687149779680702637L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        int onePageCount = Integer.parseInt(request.getParameter("onePageCount"));

        if(!HttpConfig.fake){     //真数据

            String token = TokenValue.TOKEN.getTokenValue(HttpConfig.userName,HttpConfig.passWord);
            if(token!=null) {
                String json = HttpRequest.sendGet(HttpConfig.queueListUrl, "token="+token);
//                System.out.println("接收到的："+json);

                JSONObject jsonobject = JSONObject.fromObject(json);
                JSONObject returnResult = new JSONObject();
                // 判断访问是否成功
                if(jsonobject.getString("result").equals("success")){
                    // 开始做分页工作
                    JSONArray allDataList = jsonobject.getJSONArray("data");
                    JSONArray currentDataList = PaserPage.returnCurrentDataList(currentPage,onePageCount,allDataList);
                    // 计算总页数
                    int allSize = allDataList.size();
                    int allPage = (allSize-1)/onePageCount + 1;

                    returnResult.put("result","success");
                    returnResult.put("message","获取队列成功");
                    returnResult.put("data",currentDataList);
                    returnResult.put("pageNum",allPage);

                }else{
                    returnResult.put("result","failed");
                    returnResult.put("message","获取队列失败");
                }
                // 以json字符串的形式给前端
//                System.out.println("给前端的数据"+returnResult.toString());
                response.getWriter().print(returnResult.toString());
            }

        }else{   //假数据
            JSONObject result = new JSONObject();

            ArrayList<JSONObject> queueslist = new ArrayList<>();
            for(int i =0;i<23;i++){
                JSONObject content1 = new JSONObject();
                content1.put("QUEUE_NAME","test"+i);
                content1.put("PRIO","40");
                content1.put("STATUS","Open:Active");
                content1.put("MAX","-");
                content1.put("JL/U","-");
                content1.put("JL/P","-");
                content1.put("JL/H","-");
                content1.put("NJOBS","0");
                content1.put("PEND","0");
                content1.put("RUN","0");
                content1.put("SUSP","0");
                queueslist.add(content1);
            }
            JSONArray data = JSONArray.fromObject(queueslist);

            result.put("result","success");
            result.put("message","获取队列成功");
            result.put("data",data);

            String json = result.toString();
//            System.out.println("自己封装的假的json："+json);
            // 开始做分页工作
            JSONObject jsonobject = JSONObject.fromObject(json);
            JSONArray allDataList = jsonobject.getJSONArray("data");
            JSONArray currentDataList = PaserPage.returnCurrentDataList(currentPage,onePageCount,allDataList);

            // 计算总页数
            int allSize = allDataList.size();
            int allPage = allSize/onePageCount + 1;

            JSONObject returnResult = new JSONObject();
            returnResult.put("result","success");
            returnResult.put("message","获取队列成功");
            returnResult.put("data",currentDataList);
            returnResult.put("pageNum",allPage);

            // 以json字符串的形式给前端
//            System.out.println("给前端的数据"+returnResult.toString());
            response.getWriter().print(returnResult.toString());

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}
